package person.xjl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import person.xjl.domain.Student;
import person.xjl.domain.Teacher;
import person.xjl.service.StudentService;
import person.xjl.service.TeacherService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
//把username的request生命周期换成了session有效的
@SessionAttributes("username")
public class LoginController {
    @Autowired
    private StudentService studentServiceImpl;
    @Autowired
    private TeacherService teacherServiceImpl;
//    @Autowired
//    private TeacherService  teacherServiceImpl;
//    @Autowired
//    private AdminService  adminServiceImpl;
    @RequestMapping("Login.do")
    public String login(String name, String password , int type, Model model, HttpServletResponse res){
        switch (type)
        {
            //student
            case 0:
                Student  student=studentServiceImpl.login(name,password);
                if(student==null)
                //fail
                {
                    return "login";
                }
                else
                //success
                {
                    //cookie
                    Cookie cookie=new Cookie("username",student.getName());
                    res.addCookie(cookie);
                    //变量的四种范围:page,request,session,application,30分钟,username，session变量，通行证
                    model.addAttribute("username",student.getName());
                    return "redirect:Student/list.do";
                }
                //teacher
            case 1:
                Teacher teacher=teacherServiceImpl.login(name,password);
                if(teacher==null)
                //fail
                {
                    return "login";
                }
                else
                //success
                {//变量的四种范围:page,request,session,application,30分钟,username，session变量，通行证
                    model.addAttribute("username",teacher.getName());
                    return "redirect:Student/list.do";
                }
                //admin
            case 2:

        }
       return  "";
    }
}
